package com.otaku.crawler.parser;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.otaku.crawler.bean.Location;
import com.otaku.crawler.model.FilmInfo;
import com.otaku.crawler.model.MTimePojo;
import com.otaku.crawler.model.TimeAndPrice;
import com.otaku.crawler.util.EmailHelper;
import com.otaku.helper.BaiduLocationHelper;

public class MTimeParser{
	private static Logger log = Logger.getRootLogger();
	/**
	 * 
	 * @param ds
	 * @return null means tag not match.should update this method!
	 * @author zzd
	 */
	public static MTimePojo parse(Document ds,String url) {
		try {
			MTimePojo mt = new MTimePojo();
			mt.setUrl(url);
			//first get city
			Elements cityEs = ds.select("div[id=aTheaterSubNavChangeCity]");
			
			mt.setCity(cityEs.get(0).select("span").get(0).text().trim().substring(2));
			
			//other info only need c_content
			Element cc = ds.select("div[class=c_content]").first();
			//img_src
			Element imgE = cc.select("ul[id=cinemaHeadImagesList]").first();
			mt.setImg_src(imgE.select("img[src]").first().attr("src"));
			
			//location and phone
			Element loAndTelE = cc.select("dl[class=fl w345]").first();
			mt.setLocation(loAndTelE.select("dd[class=mt9]").first().ownText());
			mt.setPhone(loAndTelE.select("dd[class=mt6]").first().ownText());
			
			//introduction
			mt.setIntroduction(cc.select("div[class=c_infocont]").first().select("h2").first().ownText());
			
			//lat and lng
			Location l = BaiduLocationHelper.getLocation(mt.getCity(), mt.getLocation());
			if (l!=null) {
				mt.setLat(l.getLat());
				mt.setLng(l.getLng());
			}else{
				log.warn("got no lat lng info.so return null");
				return null;
			}
			//get cinema list
			Elements eInter = cc.select("dl[class=s_cinamelist]");

			//filmlist can be null
			if(eInter.size()>0) mt.setFilmInfos(parseFilmInfo(eInter.get(0),url));
			else mt.setFilmInfos(null);
			
			return mt;
			
		} catch (Exception e) {
			EmailHelper.sendMsg("MTimeParser caught eception in url:\n"+url, e);
			return null;
		}
	}
	
	private static List<FilmInfo> parseFilmInfo(Element fe,String url1){
		try {
			List<FilmInfo> fil = new ArrayList<FilmInfo>();
			
			Elements tbs = fe.select("div[class=t_r]");
			
			for(Element e : tbs){
				FilmInfo fi = new FilmInfo();
				//name,url,preview,img
				Element e1 = e.select("a[class=__r_c_]").get(0);
				fi.setFilmName(e1.attr("title"));
				fi.setFilmImg(e1.select("img[class=i_img]").get(0).attr("src"));
				String url = e1.attr("href");
				fi.setUrl(url);
				fi.setPreview(url+"trailer.html");
				
				//time and yinpin
				Element e2 = e.select("span[class=fr mt6]").get(0);
				fi.setRunningTime(e2.ownText().trim());
				fi.setDoubanYinpin(e2.select("a[class=ml15]").get(0).attr("href"));
				
				//time and price
				List<TimeAndPrice> tapl = new ArrayList<TimeAndPrice>(30);
				Elements lis = e.select("ul[class=s_timelist clearfix __r_c_]").get(0).select("li:not(.ticketnonend)");
				for(Element lie : lis){
					String time = lie.select("strong").get(0).text().trim();
					String price = lie.select("em").get(0).text().trim();
					tapl.add(new TimeAndPrice(time, price));
				}
				fi.setTimeAndPrice(tapl);
				
				fil.add(fi);
			}
			
			return fil;
		} catch (Exception e) {
			EmailHelper.sendMsg("MTimeParser caught eception in url:\n"+url1, e);
			return null;
		}
	}

}
